

package cn.shengchao.examstar.product.acl.adapters.repositories.factories;

import cn.shengchao.examstar.product.domain.entity.CourseKnowledgeNode;
import cn.shengchao.examstar.product.domain.entity.Teacher;
import cn.shengchao.examstar.product.domain.valobj.Price;
import cn.shengchao.examstar.product.domain.valobj.ProductName;
import cn.shengchao.examstar.product.infrastructure.dao.po.TProductCourseNodes;
import cn.shengchao.examstar.rpc.product.req.SaveCourseNodes;

public class CourseKnowledgeNodeFactory extends AbstractProductFactory<TProductCourseNodes, CourseKnowledgeNode> {

    private TProductCourseNodes datasource;

    public static CourseKnowledgeNode of(SaveCourseNodes saveCourseNodes) {
        CourseKnowledgeNode courseKnowledgeNode = new CourseKnowledgeNode();
        courseKnowledgeNode.setId(saveCourseNodes.getId());
        courseKnowledgeNode.setCourseId(saveCourseNodes.getCourseId());
        courseKnowledgeNode.setName(ProductName.of(saveCourseNodes.getName()));
        if (saveCourseNodes.getPrice() != null) {
            courseKnowledgeNode.setPrice(Price.of(saveCourseNodes.getPrice()));
            courseKnowledgeNode.setDiscountPrice(Price.of(saveCourseNodes.getDiscountPrice()));
        }
        courseKnowledgeNode.setSort(saveCourseNodes.getSort());
        courseKnowledgeNode.setPlayUrl(saveCourseNodes.getPlayUrl());
        courseKnowledgeNode.setTeacher(Teacher.builder()
            .id(saveCourseNodes.getTeacherId())
            .headUrl(saveCourseNodes.getTeacherHeadUrl())
            .name(saveCourseNodes.getTeacherName())
            .build());
        courseKnowledgeNode.setDuration(saveCourseNodes.getDuration());
        courseKnowledgeNode.setParentId(saveCourseNodes.getParentId());
        courseKnowledgeNode.setFileList(saveCourseNodes.getFileList());
        courseKnowledgeNode.setIsLesson(saveCourseNodes.getIsLesson());
        return courseKnowledgeNode;
    }

    @Override
    protected CourseKnowledgeNode initProduct(TProductCourseNodes datasource) {
        this.datasource = datasource;
        return new CourseKnowledgeNode();
    }

    @Override
    public CourseKnowledgeNode createThumbnailProduct(TProductCourseNodes datasource) {
        CourseKnowledgeNode courseKnowledgeNode = super.createThumbnailProduct(datasource);
        courseKnowledgeNode.setDuration(datasource.getDuration());
        courseKnowledgeNode.setPlayUrl(datasource.getPlayUrl());
        courseKnowledgeNode.setTeacher(Teacher.builder()
            .id(datasource.getTeacherId())
            .headUrl(datasource.getTeacherHeadUrl())
            .name(datasource.getTeacherName())
            .build());
        courseKnowledgeNode.setParentId(datasource.getParentId());
        courseKnowledgeNode.setIsLesson(datasource.getIsLesson());
        return courseKnowledgeNode;
    }

    @Override
    public CourseKnowledgeNode afterCreateProduct(CourseKnowledgeNode product) {
        product.setCourseId(datasource.getCourseId());
        return product;
    }
}
